function contentBasedRecommendations(seedBook, books) {
    const seedBookSummary = `${seedBook.title} ${seedBook.author.join(" ")} ${seedBook.genres[0]}`;
    const similarBooks = books.filter(
        book => book.genres.includes(seedBook.genres[0]) && book.title !== seedBook.title
    );
    const similarities = similarBooks.map(book => {
        const similarity = cosineSimilarity(seedBookSummary, `${book.title} ${book.author.join(" ")} ${book.genres[0]}`);
        return { book, similarity };
    });
    return similarities.sort((a, b) => b.similarity - a.similarity).map(item => item.book);
}

// 计算两个文本的余弦相似度
function cosineSimilarity(text1, text2) {
    const v1 = textToVector(text1);
    const v2 = textToVector(text2);
    return dotProduct(v1, v2) / (magnitude(v1) * magnitude(v2));
}

// 将文本转化为向量
function textToVector(text) {
    const words = text.toLowerCase().split(" ");
    const vector = {};
    for (const word of words) {
        vector[word] = (vector[word] || 0) + 1;
    }
    return vector;
}

// 计算两个向量的点积
function dotProduct(vector1, vector2) {
    let sum = 0;
    for (const key in vector1) {
        if (vector2.hasOwnProperty(key)) {
            sum += vector1[key] * vector2[key];
        }
    }
    return sum;
}

// 计算一个向量的模长度
function magnitude(vector) {
    let sum = 0;
    for (const key in vector) {
        sum += Math.pow(vector[key], 2);
    }
    return Math.sqrt(sum);
}
